package com.example.yang.yixin.Adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yang.yixin.Activity.BankeActivity;
import com.example.yang.yixin.Activity.LoginActivity;
import com.example.yang.yixin.Activity.MeActivity;
import com.example.yang.yixin.Activity.SignInActivity;
import com.example.yang.yixin.R;
import com.example.yang.yixin.Utils.Class_info;
import com.example.yang.yixin.Utils.ScreenLock;

import java.util.ArrayList;
import java.util.List;

public class MusicGridAdapter extends RecyclerView.Adapter<MusicGridAdapter.ViewHolder> {
    private List<Class_info> classnameList;




    public MusicGridAdapter(List<Class_info> classnameList) {
        this.classnameList = classnameList;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid_music,viewGroup,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.Class_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int position=holder.getAdapterPosition();
                final Class_info class_info=classnameList.get(position);
                /*
                具体的跳转
                 */

                final ScreenLock screenLock=new ScreenLock(v.getContext());
                final String username=class_info.getusername();
                if(!screenLock.isActive()){
                    Snackbar.make(v,"未获得权限不可进入课堂！",Snackbar.LENGTH_SHORT).setAction("增加权限", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            screenLock.activeAdmin();

                        }
                    }).show();
                }
                else if(class_info.getClassname().equals("请完善个人信息后再加入班课！")){
                    Snackbar.make(v,"个人信息尚未完善！",Snackbar.LENGTH_SHORT).setAction("完善个人信息", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.setClass(v.getContext(),MeActivity.class);

                            intent.putExtra("data",username);
                            v.getContext().startActivity(intent);

                        }
                    }).show();
                }
                    else
               {
                    String Classname=class_info.getClassname();
                    String Coursename=class_info.getCoursename();
                    String bkpassword=class_info.getBkpassword();
                    Intent intent=new Intent();
                    intent.putExtra("Classname",Classname);
                    intent.putExtra("Coursename",Coursename);
                    intent.putExtra("bkpassword",bkpassword);
                    intent.putExtra("data",username);
                    intent.setClass(v.getContext(),BankeActivity.class);
                    v.getContext().startActivity(intent);
                }


            }
        });

        return holder;
    }


    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        Class_info classname=classnameList.get(position);
        viewHolder.rootname.setText(classname.getClassname());
        viewHolder.coursename.setText(classname.getCoursename());
        viewHolder.itemView.setTag(position);



    }


    @Override
    public int getItemCount() {
        return classnameList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView rootname;
        TextView coursename;
        View Class_info;

        public ViewHolder(View view){
            super(view);
            Class_info=view;
            rootname=(TextView) view.findViewById(R.id.classname);
            coursename=(TextView) view.findViewById(R.id.coursename);

        }



    }
}
